<?php

namespace App\Models;

class Stores extends Base
{
    protected $table = 'stores';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;
    const STATUS_5 = 5;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
        self::STATUS_4 => '<span class="el-tag el-tag--info">关店</span>',
        self::STATUS_5 => '<span class="el-tag el-tag--warning">装修中</span>',
    ];

    protected $fillable = [
        'id','user_id','seller_name','store_name','logo','banner','image','cate_pid','cate_id','phone','service_area',
        'service_scope','area_info','website','description','workingtime','view','collections','call','service','praise',
        'credit','sales','partners','recommend','index_recomment','index_spare','hot','quality','status','created_at','updated_at'
    ];

    protected $casts = ['banner' => 'array','image' => 'array', 'partners' => 'array'];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('store_name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['cid']) && $params['cid']) {
                    $cid = $params['cid'];
                    $query ->whereRaw("FIND_IN_SET($cid,cate_id)")->orwhereRaw("FIND_IN_SET($cid,cate_pid)");
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['recommend']) && $params['recommend']) {
                    $query ->where('recommend',$params['recommend']);
                }
                if(isset($params['index_recomment']) && $params['index_recomment']) {
                    $query ->where('index_recomment','<>',0);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('credit',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->where('id',$params['id']);
                    }
                    if(isset($params['cid']) && $params['cid']) {
                        $cid = $params['cid'];
                        $query ->whereRaw("FIND_IN_SET($cid,cate_pid)");
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $cid_id = $params['cid_id'];
                        $query ->whereRaw("FIND_IN_SET($cid_id,cate_id)");
                    }
                    if(isset($params['service_area']) && $params['service_area']) {
                        $service_area = $params['service_area'];
                        $query ->whereRaw("FIND_IN_SET($service_area,service_area)");
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['pc_recomment']) && $params['pc_recomment']) {
                        $query ->whereRaw('(hot=2 OR quality=2 OR index_recomment<>0)');
                    }
                    if(isset($params['index_recomment']) && $params['index_recomment']) {
                        $query ->where('index_recomment',$params['index_recomment']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $map = ' (store_name like "%'.$keyword.'%" or seller_name like "%'.$keyword.'%")';
                        $query->whereRaw($map);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['cid']) && $params['cid']) {
                        $cid = $params['cid'];
                        $query ->whereRaw("FIND_IN_SET($cid,cate_id)")->orwhereRaw("FIND_IN_SET($cid,cate_pid)");
                    }
                    if(isset($params['service_area']) && $params['service_area']) {
                        $service_area = $params['service_area'];
                        $query ->whereRaw("FIND_IN_SET($service_area,service_area)");
                    }
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['recommend']) && $params['recommend']) {
                        $query ->where('recommend',$params['recommend']);
                    }
                    if(isset($params['index_recomment']) && $params['index_recomment']) {
                        $query ->where('index_recomment',$params['index_recomment']);
                    }
                })->where('status','<>',-1)
                ->orderBy('created_at', 'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 手机端获取供应列表-多选
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function webLists($params = [], $fields = ['*'], $page = '', $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        $params['status'] = 1;
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->where('id',$params['id']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['cid_pid']) && $params['cid_pid']) {
                    $cid_pid = explode(",", $params['cid_pid']);
                    $map = "(";
                    foreach ($cid_pid as $v){
                        $map .= "FIND_IN_SET($v,cate_pid) OR ";
                    }
                    $map = substr($map,0,strlen($map)-3);
                    $map .=")";
                    $query ->whereRaw($map);
                }
                if(isset($params['cid_id']) && $params['cid_id']) {
                    $cid_id = explode(",", $params['cid_id']);
                    $map = "(";
                    foreach ($cid_id as $v){
                        $map .= "FIND_IN_SET($v,cate_id) OR ";
                    }
                    $map = substr($map,0,strlen($map)-3);
                    $map .=")";
                    $query ->whereRaw($map);
                }
                if(isset($params['area']) && $params['area']) {
                    $area = explode(",", $params['area']);
                    $map = "(";
                    foreach ($area as $v){
                        $map .= "FIND_IN_SET($v,service_area) OR ";
                    }
                    $map = substr($map,0,strlen($map)-3);
                    $map .=")";
                    $query ->whereRaw($map);
                }
                if(isset($params['keyword']) && $params['keyword']) {
                    $keyword = $params['keyword'];
                    $map = ' (store_name like "%'.$keyword.'%" or seller_name like "%'.$keyword.'%")';
                    $query->whereRaw($map);
                }
            })->where('status','<>',-1)
            ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        return $lists;
    }

    /**
     * 根据id查询一条供应商
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($id, $fields = ['*']){
        return $this->select($fields)->where($id)->where('status','<>',-1)->first();
    }

    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
        $fields = [ 'seller_name','store_name','cate_id','service_area','description','area_info','workingtime',
            'call','service','praise','quality','hot','index_recomment','status','created_at' ];
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params) && $params) {
                    $ids = explode('-',$params);
                    $query ->whereIn('id',$ids);
                }
            })->where('status','<>',-1)->get();

        foreach ($lists as $k=>$v){
            $cate_id = explode(',',$v['cate_id']);
            $cate_name = Categorys::whereIn('id',$cate_id)->pluck('name')->toArray();
            $v->attributes['cate_id'] = implode('、',$cate_name);
            $area_id = explode(',',$v['service_area']);
            $area_name = Areas::whereIn('id',$area_id)->pluck('name')->toArray();
            $v->attributes['service_area'] = implode('、',$area_name);

            if($v->attributes['index_recomment'] == 0){
                $v->attributes['index_recomment'] = '无';
            }else{
                $v->attributes['index_recomment'] = Categorys::where('id',$v['index_recomment'])->value('name');
            }
            if($v->attributes['hot'] == 1){
                $v->attributes['hot'] = '否';
            }else{
                $v->attributes['hot'] = '是';
            }
            if($v->attributes['quality'] == 1){
                $v->attributes['quality'] = '否';
            }else{
                $v->attributes['quality'] = '是';
            }
            if($v->attributes['status'] == 1){
                $v->attributes['status'] = '正常';
            }elseif($v->attributes['status'] == 2){
                $v->attributes['status'] = '待审核';
            }elseif($v->attributes['status'] == 3){
                $v->attributes['status'] = '未通过';
            }elseif($v->attributes['status'] == 4){
                $v->attributes['status'] = '下架';
            }elseif($v->attributes['status'] == 5){
                $v->attributes['status'] = '未提交审核';
            }
        }
        return $lists;
    }

}
